package edu.guigu.networkdemo.nonsingle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import edu.guigu.networkdemo.databinding.ItemUserBinding;
import edu.guigu.networkdemo.model.User;

public class NonSingleViewAdapter extends RecyclerView.Adapter<NonSingleViewAdapter.NonSingleViewHolder> {

    private List<User> userList = new ArrayList<>();

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    private Context context;

    public NonSingleViewAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public NonSingleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建ViewHolder
        ItemUserBinding binding = ItemUserBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        NonSingleViewHolder viewHolder = new NonSingleViewHolder(binding);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NonSingleViewHolder holder, int position) {
        // 取数据
        User user = userList.get(position);
        // ViewHolder绑定数据
        holder.binding.tvName.setText(user.getLogin());
        holder.binding.tvCreatedAt.setText(user.getType());
        holder.binding.tvUpdatedAt.setText(user.getUser_view_type());
        // 加载网络图像，设置圆角，采用Glide
        Glide.with(context)
                .load(user.getAvatar_url())
                .apply(new RequestOptions().transform(new RoundedCorners(12))) //圆角图片
                .into(holder.binding.ivAvatar);
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }

    class NonSingleViewHolder extends RecyclerView.ViewHolder {

        ItemUserBinding binding;

        public NonSingleViewHolder(@NonNull ItemUserBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
